<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flat 和 flatMap</title>
</head>

<body>
    <!--数组降维-->
    <script>
        let arr = [1, 2, 3, 4, [5, 6]];
        console.log(arr.flat());
        // [ 1, 2, 3, 4, 5, 6 ]

        arr = [1, 2, 3, 4, [5, 6, [7, 8, 9]]];
        console.log(arr.flat());
        // [1, 2, 3, 4, 5, 6, [7, 8, 9]];
        // 3维数组转1维数组
        console.log(arr.flat(2));
        // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

        // flatMap
        arr = [1, 2, 3, 4];
        let result = arr.map(item => [item * 10]);
        console.log(result);
        //[ (1) […], (1) […], (1) […], (1) […] ]
        result = arr.flatMap(item => [item * 10]);
        console.log(result);
        // [ 10, 20, 30, 40 ]
    </script>
</body>

</html>